package com.ai.trickycards.utils

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences

@SuppressLint("ApplySharedPref")
object SP {

    fun getInstance(context: Context): SharedPreferences = context.getSharedPreferences("tricky", Context.MODE_PRIVATE)

    fun putBoolean(
        context: Context,
        key: String,
        value: Boolean,
        immediately: Boolean = false
    ) {
        if (immediately) {
            getInstance(context).edit().putBoolean(key, value).commit()
        } else {
            getInstance(context).edit().putBoolean(key, value).apply()
        }
    }

    fun getBoolean(
        context: Context,
        key: String,
        defaultValue: Boolean = false
    ) = getInstance(context).getBoolean(key, defaultValue)

    fun putInt(
        context: Context,
        key: String,
        value: Int,
        immediately: Boolean = false
    ) {
        if (immediately) {
            getInstance(context).edit().putInt(key, value).commit()
        } else {
            getInstance(context).edit().putInt(key, value).apply()
        }
    }

    fun getInt(
        context: Context,
        key: String,
        defaultValue: Int = 0
    ) = getInstance(context).getInt(key, defaultValue)

    fun putFloat(
        context: Context,
        key: String,
        value: Float,
        immediately: Boolean = false
    ) {
        if (immediately) {
            getInstance(context).edit().putFloat(key, value).commit()
        } else {
            getInstance(context).edit().putFloat(key, value).apply()
        }
    }

    fun getFloat(
        context: Context,
        key: String,
        defaultValue: Float = 0f
    ) = getInstance(context).getFloat(key, defaultValue)


    fun putString(
        context: Context,
        key: String,
        value: String,
        immediately: Boolean = false
    ) {
        if (immediately) {
            getInstance(context).edit().putString(key, value).commit()
        } else {
            getInstance(context).edit().putString(key, value).apply()
        }
    }

    fun getString(
        context: Context,
        key: String,
        defaultValue: String? = null
    ) = getInstance(context).getString(key, defaultValue)
}